﻿// See https://aka.ms/new-console-template for more information
using System.Diagnostics;
using System.Reflection;






    Console.WriteLine("Hello, World!");
    try
	{
        string filePath = @"C:\path\to\output\origin.docx";
        string filePdfPath = @"C:\path\to\output\origin.pdf";
        string outputDirectory = @"C:\path\to\output";   // PDF output directory

        // 确保输出目录存在
        if (!Directory.Exists(outputDirectory))
        {
            Directory.CreateDirectory(outputDirectory);
        }
        //ConvertToPdf(filePath, outputDirectory); // convert  word document to pdf 
        ConvertPdfToImage(filePdfPath, outputDirectory);
    }
	catch (Exception ex)
	{

		throw;
	}
    static string getLibreOfficePath()
    {
        switch (Environment.OSVersion.Platform)
        {
            case PlatformID.Unix:
                return "/usr/bin/soffice";
            case PlatformID.Win32NT:
                string binaryDirectory =
          System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
                return @"C:\Program Files\LibreOffice\program\soffice.exe";
            default:
                throw new PlatformNotSupportedException("Your OS is not supported");
        }
    }

/**
 *  word documnet convert to pdf add by cfl 2024-12-23
 */
static void ConvertToPdf(string inputFilePath, string outputDirectory)
{
    string sofficePath = getLibreOfficePath(); // soffice.exe 路径

    // 确保输入文件存在
    if (!File.Exists(inputFilePath))
    {
        Console.WriteLine("输入文件不存在：" + inputFilePath);
        return;
    }

    // 构造命令行参数
    string arguments = $"--headless --convert-to pdf --outdir \"{outputDirectory}\" \"{inputFilePath}\"";

    // 设置进程信息
    ProcessStartInfo processInfo = new ProcessStartInfo
    {
        FileName = sofficePath,
        Arguments = arguments,
        RedirectStandardOutput = true,
        RedirectStandardError = true,
        UseShellExecute = false,
        CreateNoWindow = true
    };

    try
    {
        // 启动进程
        using (Process process = new Process())
        {
            process.StartInfo = processInfo;
            process.Start();

            // 读取输出和错误信息
            string output = process.StandardOutput.ReadToEnd();
            string error = process.StandardError.ReadToEnd();

            process.WaitForExit();

            // 输出结果
            if (process.ExitCode == 0)
            {
                Console.WriteLine("转换成功！");
            }
            else
            {
                Console.WriteLine("转换失败！");
                Console.WriteLine("错误信息：" + error);
            }
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine("发生异常：" + ex.Message);
    }
    
}

/**
     *  pdf convert to image add by cfl 2024-12-23
     */
static void ConvertPdfToImage(string pdfFilePath, string outputDirectory)
{
    string gsPath = @"C:\Program Files\gs\gs10.04.0\bin\gswin64c.exe"; // Ghostscript 路径

    if (!File.Exists(pdfFilePath))
    {
        Console.WriteLine("PDF 文件不存在：" + pdfFilePath);
        return;
    }

    string outputImagePattern = Path.Combine(outputDirectory, Path.GetFileNameWithoutExtension(pdfFilePath) + "_page_%03d.png");

    string arguments = $"-dNOPAUSE -dBATCH -sDEVICE=png16m -r300 -sOutputFile=\"{outputImagePattern}\" \"{pdfFilePath}\"";

    ProcessStartInfo processInfo = new ProcessStartInfo
    {
        FileName = gsPath,
        Arguments = arguments,
        RedirectStandardOutput = true,
        RedirectStandardError = true,
        UseShellExecute = false,
        CreateNoWindow = true
    };

    try
    {
        using (Process process = new Process())
        {
            process.StartInfo = processInfo;
            process.Start();

            string output = process.StandardOutput.ReadToEnd();
            string error = process.StandardError.ReadToEnd();

            process.WaitForExit();

            if (process.ExitCode == 0)
            {
                Console.WriteLine("图片转换成功：" + outputImagePattern);
            }
            else
            {
                Console.WriteLine("图片转换失败：" + error);
            }
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine("图片转换异常：" + ex.Message);
    }
}